 shortcut.add("Page_up",function() {

if(document.getElementById('blanket2').style.display == "block"){
document.getElementById('blanket2').style.display = "none";
document.getElementById('popUpDiv2').style.display = "none";
}
document.getElementById('blanket').style.display = "block";
document.getElementById('popUpDiv').style.display = "block";	
});
 
shortcut.add("Page_down",function() {
	if(document.getElementById('blanket').style.display == "block"){
document.getElementById('blanket').style.display = "none";
document.getElementById('popUpDiv').style.display = "none";
}
document.getElementById('blanket2').style.display = "block";
document.getElementById('popUpDiv2').style.display = "block";
	
});


shortcut.add("Alt+C",function() {
	newwindow = window.open('calculator','targetWindow','toolbar=no,location=no,top=200,status=no,menubar=no,scrollbars=yes,resizable=0,width=450,height=380');	
});
function Close(){
	
	document.getElementById("popUpDiv").style.display = 'none';
	document.getElementById("blanket").style.display = 'none';
	document.getElementById("popUpDiv2").style.display = 'none';
	document.getElementById("blanket2").style.display = 'none';
}

var x;
function changecolors() {
    x = 1;
    setInterval(change, 1000);
}

function change() {
	
	var m=0;
	var n=0;
	var profitloss=0;
	 $.get('./countProfitLoss',function(responseJson) {
	 for(var a in responseJson){
		 m=m+(Number(responseJson[a]["lot1"])*Number(responseJson[a]["price1"]));
		 n=n+(Number(responseJson[a]["lot1"])*Number(responseJson[a]["price2"]));
		 profitloss=m-n;
	 }
	
	 if(profitloss>0){
	
		 document.getElementById('profit').style.background ="green";
		 document.getElementById("profit").value="Profit: "+profitloss;
		 
	 }
	 else{
	
		 document.getElementById('profit').style.background = "red";
		 document.getElementById("profit").value="Loss: "+profitloss;
		 	 
	 }
	 
	 });
   
 
}

/*------------------------------------------Change color end------------------*/
function textsizer(e){

var evtobj=window.event? event : e; //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
var actualkey=String.fromCharCode(unicode);
if (actualkey=="Page_up"){
if(document.getElementById('blanket2').style.display == "block"){
document.getElementById('blanket2').style.display = "none";
document.getElementById('popUpDiv2').style.display = "none";
}
document.getElementById('blanket').style.display = "block";
document.getElementById('popUpDiv').style.display = "block";

}
if (actualkey=="Page_down"){
if(document.getElementById('blanket').style.display == "block"){
document.getElementById('blanket').style.display = "none";
document.getElementById('popUpDiv').style.display = "none";
}
document.getElementById('blanket2').style.display = "block";
document.getElementById('popUpDiv2').style.display = "block";
}
}
document.onkeypress=textsizer;

function BuySell(parameter){

if(parameter=='buy'){
document.getElementById('blanket').style.display = "block";
document.getElementById('popUpDiv').style.display = "block";
}
if(parameter=='sell'){
document.getElementById('blanket2').style.display = "block";
document.getElementById('popUpDiv2').style.display = "block";
}
}

function displayunicode(e){
changecolors();
var unicode=e.keyCode? e.keyCode : e.charCode;

}

$(document).ready( function() {
     done();
 
 $(submit).click(function(event) {
		var msg=document.getElementById("msg").innerHTML;
		var userId= ('<sec:authentication property="principal.username"/>').replace('&#32;',' ');
	if($("#ptype").val()=="Intra"){
		var a=parseInt($("#marginb").val());
		var b=parseInt($("#price").val());
		var c=parseInt($("#ql").val());
		var d=c*b;
		if(a>d)
		{
			msg+="You have buyed "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
			document.getElementById("msg").innerHTML=msg;
			var reqUrl="./order/buyOrder";
			var buyJson=JSON.stringify({ "otype":$("#otype").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),
			"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql").val(),"qw":$("#qw").val(),"price":$("#price").val(),
			"ptype":$("#ptype").val(),"status":$("#status").val(),"tp":$("#tp").val(),"accno":$("#accno").val(),
			"marginl":$("#marginlimit").val(),"cashl":$("#cashlimit").val(),"margin_b":$("#marginb").val()});
			$.ajax(
					  {
					  type: "POST",
					  url: reqUrl,
					  dataType: "json",
					  contentType: 'application/json',
					  data:buyJson ,
					  success: function(html) { 			 
					    alert(html);
					  }  
					});
		}
		else{
			alert("You have insufficient Margin Limit,Hence Can't buy.");
			 msg+="You have insufficient margin limit hence can't buy "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
			document.getElementById("msg").innerHTML=msg;
		}
	}
	 if($("#ptype").val()=="CarryForward")
	{
		var m=parseInt($("#cashlimit").val());
		var n=parseInt($("#price").val());
		var o=parseInt($("#ql").val());
		var p=n*o;
		if(m>p){
			msg+="You have buyed "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
			document.getElementById("msg").innerHTML=msg;
			var reqUrl="./order/buyOrder";
			var buyJson=JSON.stringify({ "otype":$("#otype").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),
				"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql").val(),"qw":$("#qw").val(),"price":$("#price").val(),
				"ptype":$("#ptype").val(),"status":$("#status").val(),"tp":$("#tp").val(),"accno":$("#accno").val(),
				"cashl":$("#cashlimit").val(),"marginl":$("#marginlimit").val(),"margin_b":$("#marginb").val()});
			$.ajax(
					  {
					  type: "POST",
					  url: reqUrl,
					  dataType: "json",
					  contentType: 'application/json',
					  data:buyJson ,
					  success: function(html) { 			 
					    alert(html);
					  }  
					});
		}
		else{
			alert("You have insufficient Cash Limit,Hence Can't buy.");
			 msg+="You have insufficient cash limit hence can't buy "+$("#ql").val()+" lots of "+$("#symbol").val()+".<br/>";
				document.getElementById("msg").innerHTML=msg;
				
		}
	}
	});
 
 $(save).click(function(event) { 
		var	 msg=document.getElementById("msgS").innerHTML;
		var m=0,totalLots=0;
		 $.get('./order/buyDetail',function(responseJson) {
			 	for(var a in responseJson){
					if(responseJson[a]["ptype"]==$("#ptype1").val() && responseJson[a]["symbol"]==$("#symbol").val()){
						totalLots=Number(responseJson[a]["lot1"])-Number(responseJson[a]["lot2"]);
						if(totalLots>=Number($("#ql1").val()))
						{
	 						m=1;
	 						break;
	 					}
	 				}
				}
		if(m==1){
		   msg+="You have sold "+$("#ql").val()+" lots of "+$("#symbol").val()+"<br/>";
		   document.getElementById("msgS").innerHTML=msg;
				
			var reqUrl="./order/sellOrder";
			var sellJson=JSON.stringify({"otype":$("#otype1").val(),"insname":$("#insname").val(),"symbol":$("#symbol").val(),
			"expdate":$("#expdate").val(),"qt":$("#qt").val(),"ql":$("#ql1").val(),"qw":$("#qw").val(),"price":$("#sprice").val(),
			"ptype":$("#ptype1").val(),"status":$("#status").val(),"tp":$("#tp").val(),"accno":$("#accno1").val(),
			"cashl":$("#cashl1").val(),"marginl":$("#marginl1").val(),"margin_b":$("#marginb1").val()});
				$.ajax(
		  		{
		  		type: "POST",
		  		url: reqUrl,
		  		dataType: "json",
		  		contentType: 'application/json',
		  		data:sellJson ,
		  		success: function(jsonData) { 			 
		    	alert("Hello:"+jsonData);
		  		}
				});
				}
	 		else{
	 				alert("You have not buyed sufficient lots of particular symbol");
	 				msg+="You have insufficient lots buyed hence can't sell "+$("#ql1").val()+" lots of "+$("#symbol").val()+".<br/>";
					document.getElementById("msgS").innerHTML=msg;
	 					
	 				}
	});
		 
	});	
 

 $.ajax({
		url : './mock/clientpage',
		dataType: "json",
		contentType: 'application/json',			  
		success : function(jsonData) {
			for(var a in jsonData){
				document.getElementById("clientId").value="Client ID: "+jsonData[a]["userId"];	
			document.getElementById("userId").value="Client ID: "+jsonData[a]["userId"];;
			document.getElementById("fname").value="Client Name: "+jsonData[a]["fname"];
		}
		 }		
	});



	 done1();
	 
	 $("#notifyMe").click(function(event) {
		 document.getElementById("Alertmsg").innerHTML="";
				$.ajax({ 
					type: "POST",
				url : './updateStatus1',
				dataType: "json",
				contentType: 'application/json',			  
				success : function(jsonData) {
					var msg=document.getElementById("Alertmsg").innerHTML,i=1;
					document.getElementById("alert").innerHTML=" ";
					$( "span" ).removeClass( "mes" );
					for(var a in jsonData ){
			        	msg=msg+"("+i+").  "+jsonData[a]["msg"]+".<br/>";
						document.getElementById("Alertmsg").innerHTML=msg;
						i++;
					}
				}
				});
	 });
	 
});

function done1() {
	  setTimeout( function() { 
	  updates1(); 
	  done1();
	  }, 7000);
}

function updates1() {  	
	 $.ajax({
			url : './rmsNews',
			dataType: "json",
			contentType: 'application/json',			  
			success : function(jsonData) {
				var i=0;
				for(var a in jsonData ){
					var r=confirm("You have a notification: "+jsonData[a]["msg"]);
						if (r == true) {
				        updateStatus(jsonData[a]["idStatus"]);
				        }
						else {
				     		i++;
				    	} 
				}
				if(i==0){
					$( "span" ).removeClass( "mes" );
					document.getElementById("alert").innerHTML="";	
				}
				
				else{
					$( "span" ).last().addClass( "mes" );
					document.getElementById("alert").innerHTML="  "+i+"  ";
				}
	        	
			}
	   			
		});

} 

function updateStatus(a){
	msg=JSON.stringify({ "idNum":a});
	$.ajax({ 
		type: "POST",
	url : './updateStatus',
	dataType: "json",
	contentType: 'application/json',			  
	  data:msg,
	success : function(jsonData) {	
	}
	});
	
}

function done() {

	  setTimeout( function() { 
	  updates(); 
	  
	  }, 5000);
}
function updates() {  	
	 $.get('./userAccountDetail',function(responseJson) {
			if(responseJson!=null){
			for(var a in responseJson){

					document.getElementById("cashlimit").value=responseJson[a]["cashl"];
					document.getElementById("accno").value=responseJson[a]["accno"];
					document.getElementById("accno1").value=responseJson[a]["accno"];
					document.getElementById("cashl1").value=responseJson[a]["cashl"];
					document.getElementById("marginlimit").value=responseJson[a]["marginl"];
					document.getElementById("marginl1").value=responseJson[a]["marginl"];
					document.getElementById("marginb").value=responseJson[a]["margin_b"];
					document.getElementById("marginb1").value=responseJson[a]["margin_b"];
			}
			}
			});		
}  


function oncheck(){
	if (document.form1.otype.value == "SL")
	{
	document.form1.tp.disabled = false;
	document.getElementById('status').value = 'Pending';
	}else{
	document.form1.tp.disabled = true;
	document.getElementById('status').value = 'Executed';
	}
	}
function onCheck(){
	if (document.form2.otype1.value == "SL")
	{
	document.form2.tp.disabled = false;
	document.getElementById('status1').value = 'Pending';
	}else{
	document.form2.tp.disabled = true;
	document.getElementById('status1').value = 'Executed';
	}
	}

$(function(){
    $('.source-code').each(function(index){
        var $section = $(this);
        var code =   $('#ql').updown();
        var codee =   $('#ql1').updown();
       
        // Code preview
        var $codePreview = $('<pre class="prettyprint lang-javascript"></pre>');
        $codePreview.text(code);
        $codePreview.text(codee);
        $section.html($codePreview);
        
        // Run code
        eval(code);
        eval(codee);
    });
});

